//
//  WmcHomeFlashSelectView.swift
//  YPWatermarkCamera
//
//  Created by Json on 2023/8/16.
//

import UIKit
import YPGraphicEngine
import RxRelay

class WmcHomeFlashSelectView: WmcBaseView {
    
    typealias FlashMode = YPGECamera.FlashMode

    /// stackView
    private lazy var stackView: UIStackView = {
        let stackV = UIStackView()
        stackV.axis = .horizontal
        stackV.spacing = 40.0.wmc_autoHor
        stackV.distribution = .equalSpacing
        stackV.alignment = .fill
        return stackV
    }()
    
    private lazy var buttons: [WmcFlashButton] = []
    
    private var selectedBtn: WmcFlashButton?
    
    var mode: FlashMode = .off {
        didSet {
            selectedBtn?.isSelected = false
            let button = buttons.first { $0.mode == mode }
            button?.isSelected = true
            selectedBtn = button
        }
    }
    
    let didSelectedFlashMode: PublishRelay<FlashMode> = .init()
    
    // MARK: UI
    override func makeUI() {
        super.makeUI()
        let hMargin = 24.0.wmc_autoHor
        // self
        backgroundColor = UIColor.wmc_white_FFFFFF
        layer.cornerRadius = 8.0
        clipsToBounds = true
        // stackView
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.left.equalTo(hMargin)
            make.right.equalTo(-hMargin)
            make.top.bottom.equalToSuperview()
        }
        // 添加按钮
        FlashMode.allCases.forEach { mode in
            let button = WmcFlashButton(style: .imageTop, space: 2.0)
            button.mode = mode
            stackView.addArrangedSubview(button)
            button.snp.makeConstraints { make in
                make.width.equalTo(35.0.wmc_autoHor)
            }
            self.buttons.append(button)
            button.rx.tap.subscribe(onNext: {
                [weak self] btn in
                guard let self = self else { return }
                self.mode = mode
                self.didSelectedFlashMode.accept(mode)
            }).disposed(by: rx.disposeBag)
        }
        // 默认选中
        self.mode = .off
    }

}

extension WmcHomeFlashSelectView {
    
    class WmcFlashButton: WmcStyleButton {
        
        override var isHighlighted: Bool {
            set {
                
            }
            get {
                return false
            }
        }
        
        var mode: FlashMode = .off {
            didSet {
                let image = mode.info.image
                let title = mode.info.title
                setImage(image.yp.set(tintColor: UIColor.wmc_gray_595959), for: .normal)
                setImage(image.yp.set(tintColor: UIColor.wmc_mainTheme), for: .selected)
                setTitle(title, for: .normal)
            }
        }
        
        override init(style: WmcStyleButton.Style, space: Double) {
            super.init(style: style, space: space)
            setTitleColor(UIColor.wmc_gray_595959, for: .normal)
            setTitleColor(UIColor.wmc_mainTheme, for: .selected)
            titleLabel?.font = 15.wmc_scaleFont()
            imageHeight = 32.0.wmc_autoHor
        }
        
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
    }
}
